home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Washington_1988 / DevCon88.1 / Assembler / subroutines / rnd.asm < prev    next >
Encoding:
Assembly Source File  |  1992-08-27  |  3.2 KB  |  132 lines

  1. ;
  2. ; Copyright (c) 1988 Commodore-Amiga, Inc.
  3. ;
  4. ; Executables based on this information may be used in software
  5. ; for Commodore Amiga computers.  All other rights reserved.
  6. ;
  7. ; This information is provided "as is"; no warranties are made.
  8. ; All use is at your own risk, and no liability or responsibility is assumed.
  9. ;
  10.  
  11.         CODE
  12.  
  13.         NOLIST
  14.         INCLUDE    "exec/types.i"
  15.         INCLUDE "globals.i"
  16.         LIST
  17.  
  18.         XDEF    RandomSeed,GetRandomSeed,Random
  19.  
  20. ;=============================================================================
  21. ; NAME
  22. ;    RandomSeed - seed random number generator
  23. ;
  24. ; SYSNOPSIS
  25. ;    RandomSeed( SeedValue1, SeedValue2 )
  26. ;             D0          D1
  27. ;
  28. ; FUNCTION
  29. ;    Seeds the random number generator
  30. ;
  31. ; INPUTS
  32. ;    SeedValue1 - a longword containing any value you like
  33. ;    SeedValue2 - a longword containing any value you like
  34. ;
  35. ; RESULT
  36. ;    Random number generator is initialised
  37. ;
  38. ; BUGS
  39. ;    would be tough to get bugs in this routine !!
  40. ;
  41. ; SEE ALSO
  42. ;
  43. ;============================================================================
  44. RandomSeed    movem.l    d0/d1,RND(a5)
  45.         rts
  46.  
  47. ;=============================================================================
  48. ; NAME
  49. ;    GetRandomSeed - fetch current value in random number generator
  50. ;
  51. ; SYSNOPSIS
  52. ;    RandomSeed = GetRandomSeed()
  53. ;    D0/D1
  54. ;
  55. ; FUNCTION
  56. ;    returns current value for later re-seeding to obtain the same sequence
  57. ;
  58. ; INPUTS
  59. ;    none
  60. ;
  61. ; RESULT
  62. ;    RandomSeed - a DOUBLE random seed value
  63. ;
  64. ; BUGS
  65. ;    you gotta be kidding!
  66. ;
  67. ; SEE ALSO
  68. ;
  69. ;============================================================================
  70. GetRandomSeed    movem.l    RND(a5),d0/d1
  71.         rts
  72.  
  73. ;=============================================================================
  74. ; NAME
  75. ;    Random - returns a random integer in the specified range
  76. ;
  77. ; SYSNOPSIS
  78. ;    RndNum = Random( UpperLimit )
  79. ;      D0          D0
  80. ;
  81. ; FUNCTION
  82. ;    returns a random integer in the range 0 to UpperLimit-1
  83. ;
  84. ; INPUTS
  85. ;     UpperLimit - a long(or short will do) in the range 0-65535
  86. ;
  87. ; RESULT
  88. ;    a random integer is returned to you, real quick!
  89. ;
  90. ; BUGS/LIMITATIONS
  91. ;    range was limited to 0-65535 to avoid problems with the DIVU instruction
  92. ;    which can return real wierd values if the result is larger than 16 bits.
  93. ;
  94. ; SEE ALSO
  95. ;
  96. ;============================================================================
  97. Random        move.w    d0,-(sp)    save range
  98.         beq.s    10$        range of 0 returns 0 always
  99.         bsr.s    LongRnd        get a longword random number
  100.         clr.w    d0        use upper word (it's most random)
  101.         swap    d0
  102.         divu.w    (sp),d0        divide by range...
  103.         clr.w    d0
  104.         swap    d0        ...and use remainder for the result
  105. 10$        addq.l    #2,sp        scrap range on stack
  106.         rts
  107.  
  108. ; this is the main random number generation routine. Not user callable
  109.  
  110. LongRnd        movem.l    d2-d3,-(sp)    
  111.         movem.l    RND(a5),d0/d1    D0=LSB's, D1=MSB's of random number
  112.         andi.b    #$0e,d0        ensure upper 59 bits are an...
  113.         ori.b    #$20,d0        ...odd binary number
  114.         move.l    d0,d2
  115.         move.l    d1,d3
  116.         add.l    d2,d2        accounts for 1 of 17 left shifts
  117.         addx.l    d3,d3        [D2/D3] = RND*2
  118.         add.l    d2,d0
  119.         addx.l    d3,d1        [D0/D1] = RND*3
  120.         swap    d3        shift [D2/D3] additional 16 times
  121.         swap    d2
  122.         move.w    d2,d3
  123.         clr.w    d2
  124.         add.l    d2,d0        add to [D0/D1]
  125.         addx.l    d3,d1
  126.         movem.l    d0/d1,RND(a5)    save for next time through
  127.         move.l    d1,d0        most random part to D0
  128.         movem.l    (sp)+,d2-d3
  129.         rts
  130.  
  131.         END
  132.